using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Unity.VisualScripting;
using UnityEngine;

/// <summary>
/// PlayerPrefs数据管理类
/// </summary>
public class DataManager
{
    private static DataManager instance = new DataManager();
    public static DataManager Instance {  get { return instance; } }
    private DataManager() { }

    /// <summary>
    /// 存储数据
    /// </summary>
    /// <param name="data">数据对象</param>
    /// <param name="keyName">数据对象唯一key</param>
    public void SaveData(object data, string keyName)
    {
        Type type = data.GetType();
        FieldInfo[] fields = type.GetFields();
        string key;
        foreach (FieldInfo field in fields)
        {
            key = keyName + "_" + type.Name + "_" + field.FieldType.Name + "_" + field.Name;
            SaveValue(field.GetValue(data), key);
        }
    }

    private void SaveValue(object value, string keyName)
    {
        Type type = value.GetType();
        if (type == typeof(int))
        {
            PlayerPrefs.SetInt(keyName, (int)value);
        }
        else if (type == typeof(string))
        {
            PlayerPrefs.SetString(keyName, value.ToString());
        }
        else if (type == typeof(float))
        {
            PlayerPrefs.SetFloat(keyName, (float)value);
        }
        else if (type == typeof(bool))
        {
            PlayerPrefs.SetInt(keyName, (bool)value ? 1 : 0);
        }
        //List泛型
        else if (typeof(IList).IsAssignableFrom(type)) //List继承接口IList, 判断type是否为IList的子类
        {
            //父类装子类
            IList list = value as IList;
            //存储数量
            PlayerPrefs.SetInt(keyName, list.Count);
            int index = 0;
            foreach (var item in list)
            {
                SaveValue(item, keyName + index);    //递归存储
                index++;
            }
        }
        //Dictionary
        else if (typeof(IDictionary).IsAssignableFrom(type))
        {
            IDictionary dic = value as IDictionary;
            //存储数量
            PlayerPrefs.SetInt(keyName, dic.Count);
            int index = 0;
            foreach (var key in dic.Keys)
            {
                SaveValue(key, keyName + "_key_" + index);
                SaveValue(dic[key], keyName + "_value_" + index);
                index++;
            }
        }
        //基础数据类型都不是 那么可能就是自定义数据类型
        else
        {
            SaveData(value, keyName);
        }
        PlayerPrefs.Save();
    }
    /// <summary>
    /// 读取数据
    /// </summary>
    /// <param name="type">数据类型</param>
    /// <param name="keyName">数据对象唯一key</param>
    /// <returns></returns>
    public object LoadData(Type type, string keyName)
    {
        object data = Activator.CreateInstance(type);
        FieldInfo[] fields = type.GetFields();
        string key;
        foreach (FieldInfo field in fields)
        {
            key = keyName + "_" + type.Name + "_" + field.FieldType.Name + "_" + field.Name;
            field.SetValue(data, LoadValue(field.FieldType, key));
        }
        return data;
    }

    private object LoadValue(Type field, string key)
    {
        if (field == typeof(int))
        {
            return PlayerPrefs.GetInt(key);
        }
        else if (field == typeof(string))
        {
            return PlayerPrefs.GetString(key);
        }
        else if (field == typeof(float))
        {
            return PlayerPrefs.GetFloat(key);
        }
        else if (field == typeof(bool))
        {
            return PlayerPrefs.GetInt(key) == 1 ? true : false;
        }
        //List
        else if (typeof(IList).IsAssignableFrom(field))
        {
            int count = PlayerPrefs.GetInt(key, 0);
            IList list = Activator.CreateInstance(field) as IList;
            for (int i = 0; i < count; i++) 
            {
                list.Add(LoadValue(field.GetGenericArguments()[0], key + i));//field.GetGenericArguments()[0] 获取第一个泛型类型
            }
            return list;
        }
        //Dictionary
        else if (typeof(IDictionary).IsAssignableFrom(field))
        {
            int count = PlayerPrefs.GetInt(key, 0);
            IDictionary dic = Activator.CreateInstance(field) as IDictionary;
            Type[] kvType = field.GetGenericArguments();
            for (int i = 0; i < count; i++)
            {
                dic.Add(LoadValue(kvType[0], key + "_key_" + i), LoadValue(kvType[1], key + "_value_" + i));
            }
            return dic;
        }
        //自定义类型
        else
        {
            return LoadData(field, key);
        }
    }
}
